package com.cci.kangdao.wechatOrder.controller;

import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.wechatOrder.dto.request.UserCheckRequest;
import com.cci.kangdao.wechatOrder.dto.request.WeChatWebRequest;
import com.cci.kangdao.wechatOrder.exception.NonNormalUserException;
import com.cci.kangdao.wechatOrder.exception.NotCheckException;
import com.cci.kangdao.wechatOrder.exception.RegistParameterException;
import com.cci.kangdao.wechatOrder.exception.UserCheckException;
import com.cci.kangdao.wechatOrder.service.IUserCheckService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 登陆校验
 * @author: zyf
 * @create: 2021-03-12
 */
@Controller
public class UserCheckController extends WeChatBaseController {


    private Logger log = Logger.getLogger(this.getClass().getName());

    @Resource
    private IUserCheckService userCheckService;
    /**
     * 用户校验逻辑deletePositionUserServiceStationInfo、
     */
        @RequestMapping(value = "userCheck.aspx", method = RequestMethod.GET)
    public void check(UserCheckRequest userCheckRequest,HttpServletRequest request,
                      HttpServletResponse response){
        try{
            userCheckService.checkUser(userCheckRequest,request,response);
        }catch(Exception ex){
            log.error(ex);
            String msg = null;
            if (ex instanceof NotCheckException){
                msg = "{\"status\": -99,\"msg\": \"" + ex.getMessage() + "\"}";
            }else {
                msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            }

            ParameterTool.writeResponse(response, msg);
        }
    }


    /**
     * 微信公众号-用户注册校验
     * 在注册之前先校验当前手机号是否存在或是否已关联微信号
     */
    @RequestMapping(value = "checkPhoneBeforeRegistWX.aspx", method = RequestMethod.GET)
    public void checkPhoneBeforeRegistWX(WeChatWebRequest webRequest, HttpServletRequest request,
                          HttpServletResponse response) {
        try{
        	Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            userCheckService.checkPhoneBeforeRegistWX(map);
            ParameterTool.writeSuccessResponse(response);
        }catch(RegistParameterException | NonNormalUserException | UserCheckException e){
        	ParameterTool.writeResponse(response, e.getCode(), e.getMessage());
        }catch(Exception e){
        	log.error("用户注册校验异常：message:" + e.getMessage());
        	ParameterTool.writeResponse(response, -99, e.getMessage());
        }
    }


    /**
     * 外部用户注册接口  +   登录接口
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/outsideUserRegister.aspx", method = RequestMethod.POST)
    public ModelAndView outsideUserRegister(HttpServletRequest request,
                                            HttpServletResponse response) throws Exception {

        try {

            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("response", response);

            /**
             * 微信公众号注册用户来源
             */
            int userSource = MapUtils.getIntValue(map, "userSource", -1);
            Long companyID = MapUtils.getLong(map, "companyID");
            if (userSource == 2 && null != companyID && companyID == 3) {
                String openId = request.getHeader("outSideId");
                String token = request.getHeader("Authorization");
                if (StringUtils.isBlank(openId) || StringUtils.isBlank(token)) {
                    ParameterTool.writeResponse(response, 51, "token和openId不能为空");
                    return null;
                }
                map.put("openId", openId);
                map.put("token", token);
            } else {
                String openId = request.getHeader("outSideId");
                if (StringUtils.isBlank(openId)) {
                    ParameterTool.writeResponse(response, 51, "token和openId不能为空");
                    return null;
                }
                map.put("openId", openId);
            }
            return userCheckService.outsideUserRegister(map);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);

            String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            ParameterTool.writeResponse(response, msg);
        }
        return null;
    }


    @RequestMapping(value = "weChatRefreshToken.aspx", method = RequestMethod.GET)
    public void refreshToken(WeChatWebRequest webRequest, HttpServletRequest request,
                          HttpServletResponse response) {
        try {
            userCheckService.refreshToken(request, response);
        } catch (Exception ex) {
            String msg = null;
            if (ex instanceof NotCheckException) {
                msg = "{\"status\": -99,\"msg\": \"" + ex.getMessage() + "\"}";
            } else {
                msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
            }
            ParameterTool.writeResponse(response, msg);
        }
    }

    /**
     * 微信功能公众号   用户退出的功能
     *
     * @param webRequest
     * @param request
     * @param response
     */

    @RequestMapping(value = "weChatLoginOut.aspx")
    public void weChatLoginOut(WeChatWebRequest webRequest, HttpServletRequest request,
                               HttpServletResponse response) {
        try {
            userCheckService.weChatLoginOut(request, response);
        } catch (Exception ex) {
            Map<String,Object> msg = new HashMap<>();
            if (ex instanceof NotCheckException) {
                msg.put("status",-99);
                msg.put("msg",ex.getMessage());
            } else {
                msg.put("status",-99);
                msg.put("msg","未知错误");
            }
            log.error(ex.getMessage());
            ParameterTool.writeSuccessResponse(response, msg);
        }
    }
}
